package com.moji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.entity.MjWorkOrder;
import com.moji.entity.MjWorkOrderDetail;
import com.moji.entity.SysMessage;
import com.moji.entity.dto.ReplyWorkDto;
import com.moji.mapper.MjWorkOrderDetailMapper;
import com.moji.service.MjWorkOrderDetailService;
import com.moji.service.MjWorkOrderService;
import com.moji.service.SysMessageService;
import com.moji.util.DateUtils;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 电子工单明细表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-08-20
 */
@Service
public class MjWorkOrderDetailServiceImpl extends ServiceImpl<MjWorkOrderDetailMapper, MjWorkOrderDetail> implements MjWorkOrderDetailService {

    @Resource
    private MjWorkOrderService mjWorkOrderService;

    @Resource
    private SysMessageService sysMessageService;

    @Resource
    private IdGenerator idGenerator;

    @Override
    public boolean add(ReplyWorkDto replyWorkDto) {
        MjWorkOrderDetail mjWorkOrderDetail=new MjWorkOrderDetail();
        BeanUtils.copyProperties(replyWorkDto,mjWorkOrderDetail);
        String workCode= idGenerator.idGenerator("DZGDMX");
        mjWorkOrderDetail.setWorkMxCode(workCode);
        mjWorkOrderDetail.setUserCode(MoJiCommonUtils.getUserCode());
        mjWorkOrderDetail.setUserName("[墨迹天气]");
        mjWorkOrderDetail.setCreateId(MoJiCommonUtils.getUserCode());
        mjWorkOrderDetail.setUpdateId(MoJiCommonUtils.getUserCode());
        mjWorkOrderDetail.setCreateTime(new Date());
        mjWorkOrderDetail.setUpdateTime(new Date());
        mjWorkOrderDetail.setSpareInt1(1);
        this.save(mjWorkOrderDetail);

        MjWorkOrder mjWorkOrder=mjWorkOrderService.getOne(new LambdaQueryWrapper<MjWorkOrder>()
                .eq(MjWorkOrder::getWorkCode,replyWorkDto.getWorkCode()));
        mjWorkOrder.setStatus(1);
        mjWorkOrderService.updateById(mjWorkOrder);
        //增加消息通知
        SysMessage sysMessage=new SysMessage();
        sysMessage.setReceiveUserId(mjWorkOrder.getCreateId());
        sysMessage.setSendUserId(MoJiCommonUtils.getUserCode());
        sysMessage.setMarkAsRead(0);
        sysMessage.setMessageTitle("工单回复");
        //消息类型 1-发票提醒 2-下单成功 3-待支付提醒4-超时未支付 5-工单回复 6-工单完结 7-服务到期提醒
        sysMessage.setMessageType("5");
        sysMessage.setMessageCenter("尊敬的用户您好：您于"+ DateUtils.formatDate(mjWorkOrder.getCreateTime(),DateUtils.DATE_FORMAT_EXP1) +"提交的工单已由工作人员进行回复，" +
                "工单编号为："+mjWorkOrder.getWorkCode()+"，如未能解决您的问题，请回复或及时联系客服！");
        sysMessage.setBillNo(mjWorkOrder.getId()+"");
        sysMessage.setBillTypeCode("0");
        sysMessage.setCreateId(MoJiCommonUtils.getUserCode());
        sysMessage.setUpdateId(MoJiCommonUtils.getUserCode());
        sysMessage.setCreateTime(new Date());
        sysMessage.setUpdateTime(new Date());
        sysMessageService.add(sysMessage);
        return true;
    }
}
